package com.ciwong.epaper.modules.me.ui;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.ciwong.epaper.R;
import com.ciwong.epaper.application.EApplication;
import com.ciwong.epaper.application.EpaperConstant;
import com.ciwong.epaper.modules.me.adapter.MicroMallHomePageAdapter;
import com.ciwong.epaper.modules.me.bean.Service;
import com.ciwong.epaper.modules.me.dao.MeDao;
import com.ciwong.epaper.util.BaseExtCallBack;
import com.ciwong.epaper.util.DialogUtil;
import com.ciwong.epaper.util.EConstants;
import com.ciwong.epaper.util.IntentFlag;
import com.ciwong.epaper.util.RequestCode;
import com.ciwong.epaper.util.SerializableManager;
import com.ciwong.libs.utils.CWLog;
import com.ciwong.libs.utils.CWSys;
import com.ciwong.mobilelib.i.BaseCallBack;
import com.ciwong.mobilelib.ui.BaseActivity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * 全部服务页面
 *
 * @author modify by young
 * @version ciwong v.1.0 2016年04月12日
 * @since ciwong v.1.0
 */
public class ServicesActivity extends BaseActivity {


    @Override
    protected void onResume() {
        super.onResume();
        CWLog.e("lqi", "【ServicesActivity-onResume】 ");
    }

    private MicroMallHomePageAdapter mMicroMallHomePageAdapter;
    private RecyclerView recycler_view;

    @Override
    protected int setView() {
        return R.layout.activity_services;
    }

    @Override
    protected void findViews() {
        recycler_view = (RecyclerView) findViewById(R.id.recycler_view);
    }

    @Override
    protected void initEvent() {

    }

    @Override
    protected void init() {
        setTitleText(R.string.resource_center);
        mMicroMallHomePageAdapter = new MicroMallHomePageAdapter(this);
        recycler_view.setLayoutManager(new LinearLayoutManager(this));
        recycler_view.setAdapter(mMicroMallHomePageAdapter);

    }

    @Override
    protected void loadData() {
        showMiddleProgressBar(getString(R.string.resource_center));
        //设置本ID非首次登录
        CWSys.setSharedBoolean(EConstants.SHARE_KEY_IS_FIRST_LOGIN + SerializableManager.getInstance().getUserId(true), false);
        getServices();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode,
                                    Intent data) {
        CWLog.e("TAT", "---onActivityResult");
        if (resultCode == RESULT_OK && data != null) {
            switch (requestCode) {
                case RequestCode.MainActivityResquestCode.REQUEST_CODE_TO_PAY:
                    setResult(RESULT_OK);
                    break;
                case EpaperConstant.REQUEST_CODE_CLASS_JUMP_BUY_SERVICE_PAGE:
                    //购买或者试用成功，返回书柜
                    getServices();
                    Intent intent = new Intent();
                    intent.putExtra(IntentFlag.INTENT_FLAG_OBJ, data);

                    setResult(RESULT_OK, data);
                    finish();
                    break;

            }
        }
    }

    /**
     * 获取服务列表
     */
    private void getServices() {
        MeDao.getInstance().getServices(EApplication.BRAND_ID, 0,
                new BaseExtCallBack(ServicesActivity.this, EApplication.getInstance().getUserInfoBase().getUserId() + "") {


                    @Override
                    public void failed(int errorCode, Object data) {
                        super.failed(errorCode, data);
                        showToastError(errorCode, data);
                        getServicesByCache();
                        hideMiddleProgressBar();
                    }

                    @Override
                    public void failed(Object data) {
                        super.failed(data);
                        DialogUtil.showFailedToastByErrorType(ServicesActivity.this, data);
                        getServicesByCache();
                        hideMiddleProgressBar();

                    }

                    @Override
                    public void success(Object data) {

                        CWLog.d("ciwong", "###########getServices()  success########");

                        List<Service> services = (List<Service>) data;

                        upDateMicroMallHomeList(services);

                        hideMiddleProgressBar();
                        SerializableManager.getInstance().serialize(
                                SerializableManager.SerializeKey.SHARE_KEY_SERVICE_LIST,
                                (Serializable) services, true);


                    }
                });
    }

    /**
     * 更新微商列表数据
     *
     * @param services 服务信息集合
     */
    private void upDateMicroMallHomeList(List<Service> services) {

        List<Service> normalService = new ArrayList<Service>();
        List<Service> proService = new ArrayList<Service>();

        if (null != services && services.size() > 0) {


            CWLog.d("ciwong", "###########getServices()  services.size() > 0#######");

            for (int i = 0; i < services.size(); i++) {
                if (services.get(i).ispromo()) {
                    proService.add(services.get(i));
                    CWLog.d("ciwong", "###########services.get(i).getPromotion().getEndTime()#######" + services.get(i).getPromotion().getEndTime());
                } else {
                    normalService.add(services.get(i));
                }
            }
            mMicroMallHomePageAdapter.setPromotionServiceList(proService);
            mMicroMallHomePageAdapter.setServiceList(services);
        }

    }

    /**
     * 获取缓存列表数据
     */
    private void getServicesByCache() {


        SerializableManager.getInstance().deSerialize(SerializableManager.SerializeKey.SHARE_KEY_SERVICE_LIST, new BaseCallBack() {


            @Override
            public void success(Object data) {
                if (null != data) {
                    List<Service> services = (List<Service>) data;
                    upDateMicroMallHomeList(services);
                }
            }

            @Override
            public void failed(int errorCode, Object data) {
            }

            @Override
            public void failed(Object data) {
            }
        }, true);
    }

}
